package com.dhcc.sds.busi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import com.dhcc.sds.busi.service.IAnalysisSdsCusIndstaService;
import com.dhcc.sds.custom.service.ISdsCusMrlistService;
import com.dhcc.sds.standard.dto.CdcEpisodeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 单病种质量指标统计
 *
 * @ClassName: AnalysisSdsCusIndexstaController
 * @Description: TODO
 * @author yhw
 * @date 2024-07-03 09:09:15
 */
@Controller
@RequestMapping("${sys.backendPath}/analysis/cusindexstat")
public class AnalysisSdsCusIndexstaController {

    private String PREFIX = "/backend/sds/cusindexstat/";

    @Autowired
    private ICdcHospitalService cdcHospitalService;

    @Autowired
    private ISdsCusMrlistService sdsCusMrlistService;

    @Autowired
    private ISdsQcentityService sdsQcentityService;

    @Autowired
    private IAnalysisSdsCusIndstaService analysisSdsCusIndstaService;

    @Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * @Description:自定义病种病种指标统计
     * @Title: sdscusMsta
     * @author: yhw
     * @date:2024-07-03 10:09:15
     */
    @RequestMapping("")
    public String index(Model model) {
        //获取部门信息
        JSONArray officMap = cdcHospitalService.getOfficeMap();
        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        //获取病种
        JSONArray entityMap = sdsQcentityService.getEntityMapByTypeCode("Custom");
        JSONObject obj = new JSONObject();
        obj.put("value", "");
        obj.put("text","全部");
        entityMap.remove(obj);
        model.addAttribute("officeMap",officMap);
        model.addAttribute("departMap",departMap);
        model.addAttribute("entityMap",entityMap);
        model.addAttribute("hospitalId",sdsCommonService.getHospIdBySysUser());
        return PREFIX + "cusindexstat.html";
    }
    /**
     * @Description: 查询自定义病种指标统计表格数据
     * @Title: selectIndexTableData
     * @return: Object
     * @author: yhw
     * @date:2024-07-03 12:09:15
     */
    @RequestMapping("/selectIndexTableData")
    @ResponseBody
    public Object selectIndexTableData(@RequestParam Map<String, Object> params) {
        return analysisSdsCusIndstaService.selectIndexTableData(params);
    }


    /**
     * @Description: 查询统计图数据
     */
    @RequestMapping("/selectDetails")
    @ResponseBody
    public Object selectChartDetails(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        return analysisSdsCusIndstaService.selectDetails(params);
    }

    @RequestMapping("/detailsIndex/{docId}/{locId}/{entityId}/{epIds}/{epIdsOpp}/{startDate}/{endDate}/{type}")
    public String detailsIndex(Model model, @PathVariable Long docId,@PathVariable Long locId,
                               @PathVariable Long entityId, @PathVariable String epIds,
                               @PathVariable String startDate,@PathVariable String epIdsOpp,
                               @PathVariable String endDate, @PathVariable Integer type)
    {
        model.addAttribute("docId", docId);
        model.addAttribute("locId", locId);
        model.addAttribute("entityId", entityId);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        model.addAttribute("epIds", epIds);
        model.addAttribute("epIdsOpp", epIdsOpp);
        if(type ==1){
            return PREFIX + "cusindexdetail1.html";
        }else if (type==0) {
            return PREFIX + "cusindexdetail.html";
        }
        return "";
    }

    @RequestMapping(value = "/detailslist")
    @ResponseBody
    public Object detailslist(@RequestParam Map<String, Object> params) {
        Page<CdcEpisodeDto> page = new PageFactory<CdcEpisodeDto>().defaultPage();
        List<CdcEpisodeDto> result = this.sdsCusMrlistService.pageEpisodeByDateAndFormStatus(page,params);
        page.setRecords((List<CdcEpisodeDto>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/detailslist1")
    @ResponseBody
    public Object detailslist1(@RequestParam Map<String, Object> params) {
        String type = Objects.toString(params.get("type"));
        if ("1".equals(type)){
            params.remove("epIdsOpp");
        }else if ("-1".equals(type)){
            params.remove("epIds");

        }
        Page<CdcEpisodeDto> page = new PageFactory<CdcEpisodeDto>().defaultPage();
        List<CdcEpisodeDto> result = this.sdsCusMrlistService.pageEpisodeByDateAndFormStatus(page,params);
        page.setRecords((List<CdcEpisodeDto>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }


}
